/* Copyright (c) 2006, 2009, Carl Burch. License information is located in the
 * com.cburch.logisim.Main source code and at www.cburch.com/logisim/. */
 
package com.cburch.draw.actions;

import java.util.Collection;

import com.cburch.draw.canvas.CanvasModel;
import com.cburch.draw.canvas.CanvasObject;
import com.cburch.draw.undo.Action;
import com.cburch.logisim.util.Strings;

/**
 * Abstract class for drawing actions that can occur on a CanvasModel. Each
 * subclass must replace the doSub() and undoSub() functions to make the
 * class usable. All functions contain the doIt() and undoIt() which call
 * the doSub() and undoSub() functions respectively. 
 *
 */
abstract class ModelAction extends Action {
    private CanvasModel model;
    
	/**
	 * Constructor.
	 *
	 * @return	CanvasModel		model the action is associated with
	 */
    public ModelAction(CanvasModel model) {
        this.model = model;
    }

	/**
	 * Get the name of the action.
	 *
	 * @return	String		name of action
	 */
    public abstract String getName();
    
	/**
	 * Method a subclass of ModelAction replaces to make it unique.
	 *
	 * @param	CanvasModel		model the action is associated with
	 * @return	void
	 */
    abstract void doSub(CanvasModel model);
    
	/**
	 * Method that will reverse what was done in doSub().
	 *
	 * @param	CanvasModel		model the action is associated with
	 * @return 	void
	 */
    abstract void undoSub(CanvasModel model);

	/**
	 * Calls the subclass special function.
	 *
	 * @return	void		
	 */
    public final void doIt() {
        doSub(model);
    }

	/**
	 * Calls the subclass function that undoes the classes' special function.
	 *
	 * @return	void
	 */
    public final void undo() {
        undoSub(model);
    }
    
	/**
	 * Get the model the action is assigned to.
	 *
	 * @return	CanvasModel		model action is linked to
	 */
    public CanvasModel getModel() {
        return model;
    }
    
	/**
	 * Gets the names of all CanvasObject shapes in a collection.
	 *
	 * @param	Collection		a collection of shapes
	 * @return	String		names of the shapes
	 */
    static String getShapesName(Collection coll) {
        if(coll.size() != 1) {
            return Strings.get("shapeMultiple", Strings.drawSource);
        } else {
            CanvasObject shape = (CanvasObject) coll.iterator().next();
            return shape.getDisplayName();
        }
    }
}
